/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.web.gwt.basics.client.i18n;

import org.springframework.util.StringUtils;

import com.genia.toolbox.basics.bean.AbstractEncodePropertyEditor;

/**
 * This class allows spring to generated <code>GwtI18nMessage</code> from
 * <code>String</code>.
 */
public class GwtI18nMessageEditor
    extends AbstractEncodePropertyEditor
{

  /**
   * Get a new <code>String</code> from a <code>GwtI18nMessage</code>.
   * 
   * @return the String representation of a <code>GwtI18nMessage</code>.
   */
  @Override
  public String getAsText()
  {
    GwtI18nMessage i18nMessage = (GwtI18nMessage) getValue();

    final StringBuilder stringBuilder = new StringBuilder();

    stringBuilder.append(i18nMessage.getKey());
    for (String s : encode(i18nMessage.getParameters())) {
      stringBuilder.append(SEPARATOR);
      stringBuilder.append(s);
    }

    return stringBuilder.toString();
  }



  /**
   * Generate a new <code>GwtI18nMessage</code> from a <code>String</code>.
   * 
   * @param text
   *          the <code>String</code> that represents the gwt i18n message
   */
  @Override
  public void setAsText(String text)
  {
    String[] values = decode(text.split(SEPARATOR));
    if (values.length == 0 || !StringUtils.hasText(values[0])) {
      throw new IllegalArgumentException("The permission must be of the form .*:(.*:)*");
    }

    GwtI18nMessage i18nMessage = new GwtI18nMessage(values[0]);

    if (values.length > 1) {
      String[] parameters = new String[values.length - 1];
      for (int i = 1; i < values.length; ++i) {
        parameters[i - 1] = values[i];
      }
      i18nMessage.setParameters(parameters);
    }

    setValue(i18nMessage);
  }

}
